package com.woniu.dating.service.impl;

import com.woniu.common.entity.ResultCode;
import com.woniu.common.exception.LifeException;
import com.woniu.dating.constant.DatingConstant;
import com.woniu.dating.mapper.DatingHobbyMapper;
import com.woniu.dating.pojo.DatingConcern;
import com.woniu.dating.mapper.DatingConcernMapper;
import com.woniu.dating.service.IDatingConcernService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.dating.vo.ConcernVO;
import com.woniu.dating.vo.DatingLoginVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 玖柒
 * @since 2023-05-20
 */
@Service
public class DatingConcernServiceImpl extends ServiceImpl<DatingConcernMapper, DatingConcern> implements IDatingConcernService {

    @Resource
    private DatingConcernMapper datingConcernMapper;

    @Resource
    private DatingHobbyMapper datingHobbyMapper;


    /**
     * 关注列表
     */
    @Override
    public List<ConcernVO> watchlist(DatingLoginVO datingLoginVO) {
        //1.查询出当前用户下，所有关注过的用户
        List<DatingConcern> listDatingConcern = datingConcernMapper.listDatingConcern(datingLoginVO.getUserId());
        //*遍历通过用户id，放入集合中
        List<ConcernVO> listConcernVO = null;
        for (DatingConcern datingConcern : listDatingConcern) {
            ConcernVO concernVO = datingHobbyMapper.getDatingConcernVO(datingConcern.getUserId());
            listConcernVO.add(concernVO);
        }
        //,返回关注用户信息
        return listConcernVO;
    }

    /**
     * 查看用户关注信息，避免重复点赞
     */
    @Override
    public DatingConcern getDatingConcern(DatingLoginVO datingLoginVO, Integer userId) {
        DatingConcern datingConcern = datingConcernMapper.getDatingConcern(datingLoginVO.getUserId(),userId);
        return datingConcern;
    }

    /**
     * 取消关注
     */
    @Override
    public void cancelConcern(DatingLoginVO datingLoginVO, Integer userId) {
        Integer num = datingConcernMapper.cancelConcern(datingLoginVO.getUserId(),userId);
        if (num == DatingConstant.DATING_NUM_ZERO) {
            throw new LifeException(ResultCode.DATING_CONCERN_DELETE.getMessage());
        }
    }

    /**
     * 新增关注
     * @param datingLoginVO
     * @param userId
     */
    @Override
    public void addConcern(DatingLoginVO datingLoginVO, Integer userId) {
        DatingConcern datingConcern = new DatingConcern();
        datingConcern.setCurrentId(datingLoginVO.getUserId());
        datingConcern.setUserId(userId);
        datingConcernMapper.insert(datingConcern);
    }
}
